package it.cast.wechat.service.ServiceImpl;

import it.cast.wechat.dto.OrderItemVo;
import it.cast.wechat.dto.OrderVo;
import it.cast.wechat.mapper.OrderItemMapper;
import it.cast.wechat.mapper.OrdersMapper;
import it.cast.wechat.pojo.*;
import it.cast.wechat.service.AddressService;
import it.cast.wechat.service.OrderItemService;
import it.cast.wechat.service.OrdersService;
import it.cast.wechat.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private AddressService addressService;

    @Override
    public List<OrderVo> orderList() {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdIsNotNull();

        List<Orders> orders = ordersMapper.selectByExample(example);

        List<OrderVo> orderVos = orders.stream()
                .map(Orders::builderOrderVo)
                .peek(orderVo -> {
                    List<OrderItem> orderItems = orderItemService.queryOrderItemByOrderId(orderVo.getOrderId());
                    List<OrderItemVo> orderItemVos = buildOrderItemVo(orderItems);
                    orderVo.setOrderItems(orderItemVos);
                })
                .collect(toList());
        return orderVos;
    }

    @Override
    @Transactional
    public String saveOrder(OrderVo orderVo) {
        Orders orders = orderVo.builderOrders();
        ordersMapper.insert(orders);
        //保存菜品项
        orderVo.getOrderItems().forEach(orderItemVo -> {
            OrderItem orderItem = orderItemVo.builderOrderItem();
            orderItem.setOrderId(orders.getOrderId());
            orderItemService.saveOrderItem(orderItem);
        });
        return orders.getOrderId();
    }

    @Override
    public int updateOrder(Orders orders) {
        int state = 1;
        try{
            String orderId = orders.getOrderId();
            if(orderId==null){
                System.out.println("orderId is null");
                return 0;
            }
            Orders orders1 = ordersMapper.selectByPrimaryKey(orderId);
            orders1.setState(orders.getState());
            ordersMapper.updateByPrimaryKey(orders1);
        }catch (Exception e){
            state = 0;
        }
        return state;
    }
    private List<OrderItemVo> buildOrderItemVo(List<OrderItem> orderItems){
        List<OrderItemVo> orderItemVos = orderItems.stream()
                .map(orderItem -> {
                    OrderItemVo orderItemVo = orderItem.builderOrderItemVo();
                    String productId = orderItem.getProductId();
                    //封装菜品
                    Optional<Product> opt = productService.queryProduct(productId);
                    opt.ifPresent(orderItemVo::setProduct);
                    return orderItemVo;
                })
                .collect(toList());
        return orderItemVos;
    }

    @Override
    public OrderVo queryOrderByOrderId(String orderId) {
        Orders orders = ordersMapper.selectByPrimaryKey(orderId);
        OrderVo orderVo = orders.builderOrderVo();
        List<OrderItem> orderItems = orderItemService.queryOrderItemByOrderId(orderVo.getOrderId());
        List<OrderItemVo> orderItemVos = buildOrderItemVo(orderItems);
        orderVo.setOrderItems(orderItemVos);
        //查询地址
        if(StringUtils.isNotEmpty(orders.getAddressId())){
            Address address = addressService.queryAddressByAddressId(orders.getAddressId());
            orderVo.setAddress(address);
        }
        return orderVo;
    }

    @Override
    public List<OrderVo> queryOrderByUserId(String userId, String orderId) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(StringUtils.isNotEmpty(orderId)){
            criteria.andOrderIdEqualTo(orderId);
        }
        List<Orders> orders = ordersMapper.selectByExample(example);
        return orders.stream().map(Orders::getOrderId)
                .map(orderId1 -> queryOrderByOrderId(orderId1))
                .collect(toList());
    }
}
